package com.woniuxy.controller;


import com.woniuxy.entity.User;
import com.woniuxy.exception.CustomException;
import com.woniuxy.utils.Result;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    //登录操作
    @RequestMapping("/login")
    @ResponseBody
    public Result<Object> login(User user,String captcha){//captcha验证码
        System.out.println(user);
        //认证
        //1.获取subject对象 包要导对
        Subject currentUser = SecurityUtils.getSubject();
        //2.判断是否认证
        if (!currentUser.isAuthenticated()){
            //没认证，创建令牌（携带账号密码）
            UsernamePasswordToken token =
                    new UsernamePasswordToken(user.getAccount(),user.getPwd());
            //认证
            try {
                currentUser.login(token);
                //不报异常
                System.out.println("恭喜你！");
                System.out.println("认证成功！");
            } catch (UnknownAccountException e) {
                System.out.println("账号不存在");
            }catch (IncorrectCredentialsException e){
                System.out.println("密码错误");
            }catch (Exception e){
                System.out.println("其他异常");
            }

        }
        Result<Object> result = new Result<>().setStatus(Result.SUCCESS);
        return result;

    }



    //上传文件的操作
    @RequestMapping(value="/upload",method = RequestMethod.POST)
    @ResponseBody
    //如果前端提交的key与参数名字不一致则可以通过@RequestParam获取指定key的值
    public Result<Object> uploadImage(@RequestParam("headImg")MultipartFile image,HttpServletRequest request) {

        System.out.println(image);
        //获取文件存放路径
        String path = request.getServletContext().getRealPath("");//项目根路径
        File app = new File(path);


        return null;

    }

    //注册的功能
    @RequestMapping("/regist")
    public Result<Object> regist(User user) throws CustomException {
        if (user.getAccount().contains("select")) {
            throw new CustomException("有人要搞破坏了");
        }

        return null;

    }

    //该方法用于处当前controller中所有方法抛出的异常
    @ExceptionHandler({CustomException.class})
    public String exceptionHandler(Exception e) {

        System.out.println(e.getMessage());//记录到数据库
        return "/html/responseAttack.html";

    }

    //个人中心
    @RequestMapping("/center")
    public ModelAndView center(HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView();
        User user = new User().setAccount("admin");
        //向modelAndView中放数据就是向request中放数据
        modelAndView.addObject("user",user);

        request.setAttribute("gender","man");
        //session中的拿不到

        //模拟从数据库中查询的数据
        List<String> menus = Arrays.asList("添加课程","在线考试","视频上传","视频管理");
        modelAndView.addObject( "menus",menus);

        modelAndView.setViewName("/center.html");
        return modelAndView;
    }
    /*
        springboot项目有几个默认可以放文件的地方

     */

}
